package com.power.sifa.supervise.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.plf.common.utils.NanoidUtil;
import com.power.sifa.supervise.api.IApl_feedbackdocFeign;
import com.power.sifa.supervise.model.Apl_feedbackdoc;
import com.power.sifa.supervise.model.pageInfo.Apl_feedbackdocPageInfo;
import com.power.sifa.supervise.service.Apl_feedbackdocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   处理反馈材料控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_feedbackdocController",tags={"处理反馈材料"})
public class Apl_feedbackdocController implements IApl_feedbackdocFeign {

    @Resource
    private Apl_feedbackdocService apl_feedbackdocService;

    
    @Override
    @ApiOperation("查询处理反馈材料分页列表")
    @SaCheckPermission("apl_feedbackdoc_list")
    public ResponseDTO list(Apl_feedbackdocPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_feedbackdoc> page = this.apl_feedbackdocService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_feedbackdoc apl_feedbackdoc : page) {
                apl_feedbackdoc.geneText();
                list.add(apl_feedbackdoc);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看处理反馈材料")
    @SaCheckPermission("apl_feedbackdoc_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_feedbackdoc apl_feedbackdoc  = apl_feedbackdocService.getById(id);
        if (apl_feedbackdoc != null) {
            apl_feedbackdoc.geneText();
        }
        return ResponseDTO.succData(apl_feedbackdoc);
    }

    @Override
    @ApiOperation("删除处理反馈材料")
    @SaCheckPermission("apl_feedbackdoc_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_feedbackdocService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改处理反馈材料")
    @SaCheckPermission("apl_feedbackdoc_save")
    public ResponseDTO save(Apl_feedbackdoc apl_feedbackdoc) {
        Assert.notNull(apl_feedbackdoc, "apl_feedbackdoc不能为空");
        if (ObjectUtil.isNull(apl_feedbackdoc.getId())) {
            apl_feedbackdoc.setId(apl_feedbackdoc.getApplyid() + NanoidUtil.randomNanoId());
        }
        apl_feedbackdocService.saveOrUpdate(apl_feedbackdoc);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除处理反馈材料")
    @SaCheckPermission("apl_feedbackdoc_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_feedbackdocService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_feedbackdoc_list")
    public void output(HttpServletResponse response, Apl_feedbackdocPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "处理反馈材料";
        String[] heads = {"applyid + nanoid","applyid","名称","说明","附件id","创建时间","创建人id","创建人name"};
        String[] fields = {"id","applyid","name","explain","attachids","createtime","createuserid","createusername"};
        Page<Apl_feedbackdoc> page = this.apl_feedbackdocService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "处理反馈材料";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"applyid + nanoid","applyid","名称","说明","附件id","创建时间","创建人id","创建人name"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","name","explain","attachids","createtime","createuserid","createusername"};
        List<Apl_feedbackdoc> list = (List<Apl_feedbackdoc>)ExcelUtil.Import(fields, file, Apl_feedbackdoc.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_feedbackdocService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    @Override
    public ResponseDTO queryFeedBackDocList(String applyid) {
        Assert.notNull(applyid, "applyid不能为空");
        List<Apl_feedbackdoc> feedBackDocList = this.apl_feedbackdocService.queryFeedBackDocList(applyid);
        if (CollUtil.isEmpty(feedBackDocList)) {
            // 新增一条初始化数据至数据库中然后返回
            String nowTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"));
            Apl_feedbackdoc feedbackdoc = new Apl_feedbackdoc().builder().id(NanoidUtil.randomNanoId()).applyid(applyid)
                    .name("处理反馈").explain("内容完整，真实有效").createtime(nowTime).build();
            this.apl_feedbackdocService.saveOrUpdate(feedbackdoc);
            return ResponseDTO.succData(Collections.singletonList(feedbackdoc));
        }
        return ResponseDTO.succData(feedBackDocList);
    }

}
